﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Chilkat;

namespace SeriousTalker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged, IDisposable
    {
        private string _address;
        private string _messageToUser;
        private string _message;
        private string _signature;
        private ObservableCollection<Message> _dialog;
        private Client _client;
        private Server _server;

        public event PropertyChangedEventHandler PropertyChanged;
        private void Notify(string info)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public string Signature
        {
            get { return _signature; }
            set { _signature = value; Notify("Signature"); }
        }

        public string Address
        {
            get { return _address; }
            set { _address = value; Notify("Address"); }
        }

        public string MessageToUser
        {
            get { return _messageToUser; }
            set { _messageToUser = value; Notify("MessageToUser"); }
        }

        public string Message
        {
            get { return _message; }
            set { _message = value; Notify("Message");}
        }

        public ObservableCollection<Message> Dialog
        {
            get { return _dialog; }
            set { _dialog = value; Notify("Dialog"); }
        }

        public MainWindow()
        {
            InitializeComponent();
            _client = new Client();
            _server = new Server(true);
            _dialog = new ObservableCollection<Message>();
            MainGrid.DataContext = this;

            Address = "localhost";
            _server.NewMessage += new MessageReceived(NewMessage);
            _server.NewConnection += delegate
                                         {
                                             MessageToUser = "New Connection!";
                                         };
        }

        private void NewMessage(object sender, EventArgs e)
        {
            var message = _server.Message.Split(';');
            lbMessages.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                         new Action(() => Dialog.Add(new Message(DateTime.Now, message[1], message[0]))));
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            if(Address != "")
            {
                try
                {
                    _client.Connect(Address);
                }
                catch(Exception ex)
                {
                    MessageToUser = ex.Message;
                }
            }
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            var message = new Message(DateTime.Now, (Signature != ""? Signature : "anonymous"), Message);
            Dialog.Add(message);
            if (Message != "")
            {
                try
                {
                    _client.SendMessage(Message, Signature);
                }
                catch(Exception ex)
                {
                    MessageToUser = ex.Message;
                }
            }
        }


        public void Dispose()
        {
            _server.Dispose();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Dispose();
        }
    }
}
